home *** CD-ROM | disk | FTP | other *** search
- /***
- ***
- *** To send data between stream sockets (having communication style
- *** SOCK_STREAM)
- *** Code based on an example taken from SUN® Network Programming Guide,
- *** chapter 10, A Socket-Based Interprocess Communications Tutorial,
- *** Revision A, of 27 March 1990, Fig. 10-9, pp. 266-267.
- ***
- ***/
-
- #ifdef SUN_C
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdio.h>
- #else SUN_C
- /*
- #define MAC_APPL
- */
- #include <console.h>
- #include "MacSocket.h"
- #define bcopy(s1, s2, n) memcpy((s2), (s1), (n))
- #endif SUN_C
-
- #define Port (31768)
- #define DATA "\nFrom: client\nTo: server\nMssg: Hello server!\n"
-
- main()
- {
- int sock, mssgSize;
- struct sockaddr_in server;
- union
- {
- long address;
- unsigned char domain[4];
- } host;
- char buf[1024];
-
- #ifdef MAC_APPL
- /* prevent showing informative MacTCP/IP messages */
- SetDialogShowTime(0);
- #endif MAC_APPL
-
- /* Create socket. */
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock < 0)
- {
- perror("opening stream socket");
- exit(1);
- }
- /* Connect socket using address 192.6.1.30 and port# */
- server.sin_family = AF_INET;
- host.domain[0] = 192;
- host.domain[1] = 6;
- host.domain[2] = 1;
- host.domain[3] = 30;
- bcopy((char*)&host.address, (char*)&server.sin_addr, 4);
- server.sin_port = Port;
-
- if (connect(sock, (struct sockaddr_in *)&server, sizeof server) < 0)
- {
- perror("connecting stream socket");
- exit(1);
- }
- if (send(sock, DATA, sizeof DATA, 0) < 0)
- {
- perror("writing on stream socket");
- exit(1);
- }
- if ((mssgSize = recv(sock, buf, sizeof buf, 0)) < 0)
- {
- perror("reading on stream socket");
- exit(1);
- }
- buf[mssgSize] = '\0';
- printf("Client--> %s\n", buf);
- close(sock);
- exit(0);
- }
-